package com.uh.calculation.inter.bathImp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uh.calculation.batchOperator.*;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.SqlWriteBean;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.inter.Warn;
import com.uh.calculation.operator.*;
import com.uh.calculation.util.GetOrganization;
import com.uh.microservice.core.dao.JdbcDao;
import com.uh.summary.enums.RuleEnum;
import com.uh.summary.mapper.BathMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class Bath_S_MaoyiImp implements Warn {



    private JdbcDao jdbcDao;

    public WarnJiSuanBean warnJiSuan = null;
    private BathMapper bathMapper;

    List<Map<String, Object>> S204ls = null;
    List<Map<String, Object>> S203ls = null;
    List<Map<String, Object>> S204_1ls = null;
    List<Map<String, Object>> S104ls = null;
    List<Map<String, Object>> HJ_ls = null;
    List<Map<String, Object>> HJN_ls = null;
    List<Map<String, Object>> F201_1ls = null;


    //1-本月 营业收入修改比例超过15%，计预警因子50。
    //年报营业收入修改比例超过15%，计预警因子50。（此条仅用于4月年报出来时，但一直触发到该企业被核查或检查为止）
    private String  xgfd="[{dimension:\"A\",field:\"$NEW_VALUE;$NEW_VALUE#m\",formulaBeans:[{formula:\"($NEW_VALUE@$NEW_VALUE#m)/$NEW_VALUE#m>0.15|50\"}]}]";

    private String  xgcs="[{dimension:\"A\",field:\"$NEW_VALUE\",formulaBeans:[{formula:\"$NEW_VALUE>3|50\"}]}]";


    //每季末的1-本月营业额(s204-1 M6)/季报上的营业收入(e203 M26)为115%时，计100分，每比115%大一个百分点，加2分。为85%时，计100分，每小85%一个百分点，加2分。（该条只会在每季季报出来后才会触发，一旦触发，持续到该企业被核查或检查）
    private String pp2 = "[{dimension:\"E\",field:\"S204-1_M6;S203_M26\",formulaBeans:[{formula:\"S204-1_M6/S203_M26>=1.15&((((S204-1_M6/S203_M26)-1.15)*100)*2)+100>=200|200\"},{formula:\"S204-1_M6/S203_M26>=1.15&((((S204-1_M6/S203_M26)-1.15)*100)*2)+100<200|((((S204-1_M6/S203_M26)-1.15)*100)*2)+100\"},{formula:\"S204-1_M6/S203_M26<=0.85&((((S204-1_M6/S203_M26)@0.85)*100)*2)+100>=200|200\"},{formula:\"S204-1_M6/S203_M26<=0.85&((((S204-1_M6/S203_M26)@0.85)*100)*2)+100<200|((((S204-1_M6/S203_M26)@0.85)*100)*2)+100\"}]}]";


    //贸易-住餐
    private String zc = "[{dimension:\"B\",field:\"S104-1_M1\",formulaBeans:[{formula:\"S104-1_M1<2400|50\"}]}]";


    @Override
    public Integer jisuan(WarnJiSuanBean warnJiSuan) {
        this.warnJiSuan = warnJiSuan;
        this.jdbcDao = warnJiSuan.getJdbcDao();
        this.bathMapper=warnJiSuan.getBathMapper();
        GetOrganization getOrganization = new GetOrganization(jdbcDao);

        try{
            S204ls = bathMapper.getSlist(warnJiSuan.getSt(), warnJiSuan.getOffset());
            warnJiSuan.setIsSelect(true);
            //getOrganization.getBatchFieldName("E204-1", st);
            for(Map<String, Object> m :S204ls){
                SqlWriteBean sqlWriteBean  =  new SqlWriteBean();
                JSONArray jsonArray204=null;
                JSONArray jsonArray203=null;
                JSONArray jsonArray104=null;
                JSONArray jsonArrayHJ_ls=null;
                JSONArray jsonArrayHJN_ls=null;
                JSONArray jsonArray201 = null;



                if( m.get("et204Res")!=null){
                    String topicResponsibles = m.get("et204Res").toString();
                    jsonArray204 = JSONArray.parseArray("[" + topicResponsibles + "]");
                }
                if( m.get("et203Res")!=null) {
                    String et203Res = m.get("et203Res").toString();
                    jsonArray203 = JSONArray.parseArray("[" + et203Res + "]");
                }
                if (m.get("et201Res") != null) {
                    String et201Res = m.get("et201Res").toString();
                    jsonArray201 = JSONArray.parseArray("[" + et201Res + "]");
                }
                if( m.get("et104Res")!=null) {
                    String et104Res = m.get("et104Res").toString();
                    jsonArray104 = JSONArray.parseArray("[" + et104Res + "]");
                }
                if( m.get("ethjRes")!=null) {
                    String ethjRes = m.get("ethjRes").toString();
                    jsonArrayHJ_ls = JSONArray.parseArray("[" + ethjRes + "]");
                }
                if( m.get("ethjnRes")!=null) {
                    String ethjnRes = m.get("ethjnRes").toString();
                    jsonArrayHJN_ls = JSONArray.parseArray("[" + ethjnRes + "]");
                }

                sqlWriteBean.setJsonArray201(jsonArray201);



                HjBathOperator hjOperator = new HjBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                List<OperatorVal> list = JSONObject.parseArray(xgfd, OperatorVal.class);
                //修改一
                warnJiSuan.setIs_hc(false);
                warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
                hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;商品销售额;千元");
                hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;商品销售额;千元");
                hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;零售额;千元");
                hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;零售额;千元");
                hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;零售额;千元");

                warnJiSuan.setIs_hc(true);
                hjOperator.jisuan(jsonArrayHJN_ls, list,"本年;商品销售额;零售额;千元");
                hjOperator.jisuan(jsonArrayHJN_ls, list,"本年;服务营业额;千元");




                list = JSONObject.parseArray(xgcs, OperatorVal.class);
                HjBathNumOperator hjNumOperator = new HjBathNumOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
                warnJiSuan.setIs_hc(false);
                hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;商品销售额;千元");
                hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;商品销售额;千元");
                hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;零售额;千元");
                hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;零售额;千元");

                warnJiSuan.setIs_hc(true);
                hjNumOperator.jisuan(jsonArrayHJN_ls, list,"本年;商品销售额;零售额;千元");
                hjNumOperator.jisuan(jsonArrayHJN_ls, list,"本年;服务营业额;千元");
                hjNumOperator.jisuan(jsonArrayHJN_ls, list,"本年;商品销售额;批发额;千元");
                hjNumOperator.jisuan(jsonArrayHJN_ls, list,"本年;商品销售额;批发额;千元");




                //启动季报与月报匹配算子
                JtYBathOperator jtYOperator = new JtYBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                list = JSONObject.parseArray(pp2, OperatorVal.class);
                //匹配二
                warnJiSuan.setIs_hc(false);
                warnJiSuan.setWarnType(RuleEnum.TRADE_MATCH_FACTOR_DATA.getCode());//风险因子类型
                jtYOperator.jisuan(jsonArray203, jsonArray204, list);

                TjryBathOperator tjryOperator = new TjryBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                warnJiSuan.setIs_hc(false);
                warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_STATISTICS_FACTOR_DATA.getCode());//风险因子类型
                tjryOperator.jisuan(jsonArray204, "M5", "B");


                //小升规  贸易-住餐
                XsgBathOperator xsgOperator = new XsgBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                list = JSONObject.parseArray(zc, OperatorVal.class);
                warnJiSuan.setIs_hc(false);
                warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_SMALL_FACTOR_DATA2.getCode());//风险因子类型
                xsgOperator.jisuanMaoyiZc(jsonArray104, list);


            }
        }catch (Exception e){
            e.printStackTrace();

        }

        return  null;
    }
}
